package io.gitee.thinkbungee.adaptor;

import io.gitee.thinkbungee.entity.mongo.MongoDefinition;
import io.gitee.thinkbungee.utils.mongo.MongoType;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mongo 表信息适配器
 *
 * @author chenyichang
 * @date 2022/5/24
 */
public class MongoTableInfoAdaptor {

    /**
     * mongo只能获取到表名，其他需要手动填写（保持和关系型数据库一致）
     *
     * @param names 表名
     */
    public static List<Map<String, String>> tableInfo(List<String> names) {
        List<Map<String, String>> result = new ArrayList<>(names.size());
        for (String name : names) {
            result.add(tableInfo(name));
        }
        return result;
    }

    public static Map<String, String> tableInfo(String name) {
        Map<String, String> tableInfo = new HashMap<>(4 * 4 / 3 + 1);
        tableInfo.put("engine", "mongo无引擎");
        tableInfo.put("createTime", "mongo无法查询创建时间");
        tableInfo.put("tableComment", "mongo无备注");
        tableInfo.put("tableName", name);
        return tableInfo;
    }

    /**
     * 列名适配（保持和关系型数据库一致）
     * 此方法只针对主Bean
     */
    public static List<Map<String, String>> columnInfo(MongoDefinition mongoDefinition) {
        List<MongoDefinition> child = mongoDefinition.getChild();
        List<Map<String, String>> result = new ArrayList<>(child.size());
        for (MongoDefinition md : child) {
            Map<String, String> map = new HashMap<>(5 * 4 / 3 + 1);
            map.put("extra", md.isArray() ? "array" : "");
            map.put("columnComment", "");
            map.put("dataType", md.hasChild() ? md.getPropertyName() : MongoType.typeInfo(md.getType()));
            map.put("columnName", md.getPropertyName());
            map.put("columnKey", StringUtils.equals(md.getPropertyName(), "_id") ? "PRI" : "");
            result.add(map);
        }
        return result;
    }


}
